package com.erclab.internal.xpresso.security;

import java.util.HashMap;

import com.erclab.internal.xpresso.dataAccess.Query;
import com.erclab.internal.xpresso.datavalidators.DataValidationError;
import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.formelements.FormElement;
import com.erclab.internal.xpresso.forms.Form;
import com.erclab.internal.xpresso.forms.FormHelper;

public class FormUpdateProtector implements FormHelper {

	// TODO: Change this when Form Parameters are added
	protected String USERNAME_FIELD = "UserName";
	
	public void beforeFormCreation(Form theForm) throws FormException {
		if (theForm.getFormParameters().getAction().equals("update")){
			String destinationTable = getDestinationTable(theForm);
			String theQuery = "SELECT " + theForm.getUniqueKeyName() +  " , " + USERNAME_FIELD +
				" FROM " + destinationTable + " WHERE " + theForm.getUniqueKeyName() + "=?";
			Query q = new Query();
			q.setQuery(theQuery);
			q.setValues(new String[]{theForm.getFormParameters().getUniqueKeyValue()});
			
			
		}
	}
	
	
	protected String getDestinationTable(Form theForm) {
		String destinationTable = ((FormElement)theForm.getFormElements().get(0)).getDestinationTable();
		return destinationTable;
	}


	public void afterFormCreation(Form theForm) throws FormException {
		// TODO Auto-generated method stub

	}

	public void afterModification(Form theForm, HashMap keyValues)
			throws FormException, DataValidationError {
		// TODO Auto-generated method stub

	}

	public void afterSubmission(Form theForm, HashMap keyValues)
			throws FormException, DataValidationError {
		// TODO Auto-generated method stub

	}

}
